package com.seng.userservice.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seng.userservice.entity.TbUser;
import com.seng.userservice.service.AliMsgService;
import com.seng.userservice.service.TbUserService;
import com.seng.utilscommon.alimsgutil.RandomUtil;
import com.seng.utilscommon.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api(value = "用户管理")
@RestController
@RequestMapping("/userservice/tb-user")
@CrossOrigin
public class LoginController {
    @Resource
    private TbUserService tbUserService;

    @Resource
    private AliMsgService aliMsgService;

    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result login(){
        return Result.ok().data("token","rms");
    }

    @ApiOperation(value = "登录之后查询用户信息")
    @GetMapping("/getInfo")
    public Result getInfo(){
        return Result.ok().data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif").data("name","冉孟森");
    }

    @ApiOperation(value = "查询所有学生信息")
    @PostMapping("/findAll/{current}/{size}")
    public Result findAll(@PathVariable("current") Integer current,
                          @PathVariable("size") Integer size,
                          @RequestBody(required = false) TbUser tbUserVO){
        Page<TbUser> page = new Page<>(current,size);
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u.deleted",0);
        if (!StringUtils.isEmpty(tbUserVO.getName())){
            queryWrapper.like("d.name",tbUserVO.getName());
        }
        if (!StringUtils.isEmpty(tbUserVO.getUsername())){
            queryWrapper.like("username",tbUserVO.getUsername());
        }
        if (!StringUtils.isEmpty(tbUserVO.getNickname())){
            queryWrapper.like("nickname",tbUserVO.getNickname());
        }
        if (!StringUtils.isEmpty(tbUserVO.getEmail())){
            queryWrapper.like("email",tbUserVO.getEmail());
        }
        if (!StringUtils.isEmpty(tbUserVO.getSex()) && tbUserVO.getSex() != 2){
            queryWrapper.like("sex",tbUserVO.getSex());
        }
        IPage<TbUser> studentPage = tbUserService.findStudentPage(page, queryWrapper);
        return Result.ok().data("users",studentPage);
    }

    @ApiOperation(value = "注销用户")
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok();
    }

    @ApiOperation(value = "短信验证接口")
    @PostMapping("/sendMsg")
    public Result sendMsg(String phoneNumber){
        //1.直接从redis中获取验证码
        Object code = redisTemplate.opsForValue().get(phoneNumber);
        if (!StringUtils.isEmpty(code)){
            return Result.ok();
        }else {
            String fourBitRandom= RandomUtil.getFourBitRandom();
            //如果不存在,在获取验证码时候需要放入redis中
            redisTemplate.opsForValue().set(phoneNumber,fourBitRandom,1, TimeUnit.MINUTES);
            Map<String,String> map=new HashMap<String,String>();
            map.put("code",fourBitRandom);
            String templateParam= JSON.toJSON(map).toString();
            boolean sms = aliMsgService.SendSms(phoneNumber, templateParam);
            if (!sms){
                return Result.error();
            }else {
                return Result.ok();
            }
        }
    }
}
